/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityEthnicityInfo;
import org.kuali.rice.kim.util.KimConstants;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityEthnicityInfoMapper implements ContextMapper {

  private final Log log = LogFactory.getLog(getClass());
  private String entityIdAttributeName = null;
  private String ethnicityCodeAttributeName = null;
  private String subEthnicityCodeAttributeName = null;
  private String personalSuppressionAttributeName = null;

  //beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityEthnicityInfo keei = new KimEntityEthnicityInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;
      // currently, this assumes that an entity only has one ethnicity (the one aligned
      // with this ldap record.  this is a decent assumption for now, but a configurable
      // option for either doing it this way or mapping multiple ethnicities would be better
      // perhaps some organizations have multi-valued attributes that list ethnicities

      keei.setEntityId(LdapUtilities.materialize(context.getStringAttribute(entityIdAttributeName)));
      // the id is a synthetic identifier for the instance of an entity ethnicity.
      // this should be unique in the system, but there is no graceful way to do this
      // from an ldap mapping
      keei.setId("1");//is there an ldap attribute for this?
      keei.setEthnicityCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(ethnicityCodeAttributeName)));
      keei.setSubEthnicityCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(subEthnicityCodeAttributeName)));
      String suppressionAttributeValue = context.getStringAttribute(personalSuppressionAttributeName);
      if (suppressionAttributeValue != null && suppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        keei.setSuppressPersonal(true);
        keei.setEthnicityCode(KimConstants.RESTRICTED_DATA_MASK);
        keei.setSubEthnicityCode(KimConstants.RESTRICTED_DATA_MASK);
      } else {
        keei.setSuppressPersonal(false);
        keei.setEthnicityCode(LdapUtilities.materialize(context.getStringAttribute(ethnicityCodeAttributeName)));
        keei.setSubEthnicityCode(LdapUtilities.materialize(context.getStringAttribute(subEthnicityCodeAttributeName)));
      }
    } catch (Throwable t) {
      log.error("Error in ContextMapper", t);
      String msg = "ContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped LdapKimEntityEthnicity:"
              + "(entityId=" + keei.getEntityId() + ")"
              + "(id=" + keei.getId() + ")");
    }
    return keei;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getEntityIdAttributeName() {
    return this.entityIdAttributeName;
  }

  public void setEntityIdAttributeName(String attrName) {
    this.entityIdAttributeName = attrName;
  }

  public String getEthnicityCodeAttributeName() {
    return this.ethnicityCodeAttributeName;
  }

  public void setEthnicityCodeAttributeName(String attrName) {
    this.ethnicityCodeAttributeName = attrName;
  }

  public String getSubEthnicityCodeAttributeName() {
    return this.subEthnicityCodeAttributeName;
  }

  public void setSubEthnicityCodeAttributeName(String attrName) {
    this.subEthnicityCodeAttributeName = attrName;
  }

  public String getPersonalSuppressionAttributeName() {
    return this.personalSuppressionAttributeName;
  }

  public void setPersonalSuppressionAttributeName(String attrName) {
    this.personalSuppressionAttributeName = attrName;
  }

  private void validateConfiguration() {
    if (entityIdAttributeName == null) {
      String msg = "The property 'entityIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (ethnicityCodeAttributeName == null) {
      String msg = "The property 'ethnicityCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (subEthnicityCodeAttributeName == null) {
      String msg = "The property 'subEthnicityCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (personalSuppressionAttributeName == null) {
      String msg = "The property 'personalSuppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods
}
